//
//  KLLeftTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/4/26.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLLeftTableViewController.h"
#import "KLLeftHead.h"
#import "SDImageCache.h"
#import "HemaConst.h"
#import "UIImageView+WebCache.h"
#import "FMDBTool.h"
#import "KLHelp.h"
#import "JRToast.h"
#import "KLHttpAPI.h"
#import "KLUserInformationTableViewController.h"
#import "KLSettingTableViewController.h"

@interface KLLeftTableViewController ()
{
    UIImageView *imageView;
}

@property (nonatomic,strong) NSArray *imageArray;
@property (nonatomic,strong) NSArray *labelArray;

@property (nonatomic,strong) NSMutableDictionary *userInforDic;
@end

@implementation KLLeftTableViewController

-(NSArray *)imageArray{
    if(!_imageArray){
        _imageArray = [NSArray arrayWithObjects:@"钥匙",@"锁",@"钱包",@"命令",@"申请钥匙",@"设置",nil];
    }
    
    return _imageArray;
}

-(NSArray *)labelArray{
    if(!_labelArray){
        _labelArray = [NSArray arrayWithObjects:@"我的钥匙",@"我的锁",@"我的钱包",@"委托命令",@"申请钥匙",@"设置",nil];
    }
    
    return _labelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.scrollEnabled = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUpdate:) name:@"imageUpdate" object:nil];
    
    //侧滑栏导航栏隐藏
    self.navigationController.navigationBarHidden = YES;
    //拿到数据库数据
    [FMDBTool openDataBase];
    self.userInforDic = [FMDBTool selectDataBase];
    
    //表头视图的设置
    KLLeftHead *leftHeadView = [[KLLeftHead alloc]initWithFrame:CGRectMake(0, 0, UI_View_Width, 120)];
    self.tableView.tableHeaderView = leftHeadView;
    [self tableviewHeadSet:leftHeadView];
    
    UIImageView *leftBackGround = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"测滑背景"]];
    leftBackGround.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.tableView setBackgroundView:leftBackGround];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:1];
    UILabel *label = (UILabel *)[cell viewWithTag:2];

    cellImageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    label.text = self.labelArray[indexPath.row];
//    label.textColor = RGB_UI_COLOR(238, 203, 173);
    label.font = [UIFont fontWithName:@"Copperplate-Bold" size:16];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 5){
        NSLog(@"点击了“设置”,发送通知");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushSettingVC" object:nil];
        return ;
    }
    
    if(![KLHelp isConnectionAvailable]){
        [JRToast showWithText:@"请检查网络连接" topOffset:250];
        return ;
    }
    
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"loginInitSuccess"] == false){
        [JRToast showWithText:@"当前网速较慢，请耐心等待初始化或重新登陆"];
        return ;
    }
    
    if(indexPath.row == 0){
        NSLog(@"点击了“我的钥匙”,发送通知");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushMyKeyVC" object:nil];
    }else if(indexPath.row == 1){
        NSLog(@"点击了“我的锁”,发送通知");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushMyLockVC" object:nil];
    }else if(indexPath.row == 2){
        NSLog(@"点击了“我的钱包”，发送通知");
        [KLHttpAPI checkIsExitPaypwdCompletionHandler:^(MKNetworkOperation *operation) {
            [[NSUserDefaults standardUserDefaults] setObject:[operation.responseJSON objectForKey:@"success"] forKey:@"isPaypwdExit"];
        } errorHandler:^(NSString *error) {
        }];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushMyWalletVC" object:nil];
    }else if(indexPath.row == 3){
        NSLog(@"点击了“委托命令”,发送通知");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"pushCommondsVC" object:nil];
    }else if (indexPath.row == 4){
        NSLog(@"点击了“申请钥匙”，发送通知");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushApplyForKeyVC" object:nil];
    }
}

//表头视图的设置
-(void)tableviewHeadSet:(UIView *)leftHeadView{
    //用户头像
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 50, 50)];
    [imageView sd_setImageWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"] placeholderImage:[UIImage imageNamed:@"我的_on"]];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 25;
    [leftHeadView addSubview:imageView];
    
    //用户昵称
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 45, 120, 40)];
    nameLabel.text = [self.userInforDic objectForKey:@"username"];
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentLeft;
    [leftHeadView addSubview:nameLabel];
    //编辑按钮
    UIButton *editBtn = [[UIButton alloc]initWithFrame:CGRectMake(200, 52, 24, 26)];
    [editBtn setBackgroundImage:[UIImage imageNamed:@"my_edit"] forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(editUserInformation:) forControlEvents:UIControlEventTouchUpInside];
    [leftHeadView addSubview:editBtn];
    
    //去掉表尾的多余cell
    self.tableView.tableFooterView = [[UIView alloc]init];
}-(void)editUserInformation:(id)sender{
//    //侧滑栏翻页切换用户端效果
//    [UIView beginAnimations:nil context:nil];
//    //持续时间
//    [UIView setAnimationDuration:1.0];
//    //在出动画的时候减缓速度
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//    //添加动画开始及结束的代理
//    [UIView setAnimationDelegate:self];
//    //    [UIView setAnimationWillStartSelector:@selector(begin)];
//    //    [UIView setAnimationDidStopSelector:@selector(stopAni)];
//    //动画效果
//    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
//    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:1];
//    [UIView commitAnimations];
//    return;
    
    if(![KLHelp isConnectionAvailable]){
        [JRToast showWithText:@"请检查网络连接" topOffset:250];
        return ;
    }
    
    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    UINavigationController *userInformationVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"userInformationNav"];
    [self presentViewController:userInformationVC animated:YES completion:nil];
}

-(void)imageUpdate:(id)sender{
    [imageView sd_setImageWithURL:[[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"] placeholderImage:[UIImage imageNamed:@"我的_on"]];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
